﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class MobileInfo
    {
        public int Add(KeRui.Model.MobileInfo model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@Version", SqlDbType.NVarChar, 50), new SqlParameter("@Name", SqlDbType.NVarChar, 100), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@Way", SqlDbType.Int, 4) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Version;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.Way;
            DbHelperSQL.RunProcedure("MobileInfo_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            DbHelperSQL.RunProcedure("MobileInfo_Delete", parameters, out num);
        }

        public bool Exists(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            return (DbHelperSQL.RunProcedure("MobileInfo_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.MobileInfo GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.MobileInfo info = new KeRui.Model.MobileInfo();
            DataSet set = DbHelperSQL.RunProcedure("MobileInfo_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    info.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                info.Version = set.Tables[0].Rows[0]["Version"].ToString();
                info.Name = set.Tables[0].Rows[0]["Name"].ToString();
                if (set.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    info.Type = int.Parse(set.Tables[0].Rows[0]["Type"].ToString());
                }
                if (set.Tables[0].Rows[0]["Way"].ToString() != "")
                {
                    info.Way = int.Parse(set.Tables[0].Rows[0]["Way"].ToString());
                }
                return info;
            }
            return null;
        }

        public void Update(KeRui.Model.MobileInfo model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@Version", SqlDbType.NVarChar, 50), new SqlParameter("@Name", SqlDbType.NVarChar, 100), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@Way", SqlDbType.Int, 4) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Version;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.Way;
            DbHelperSQL.RunProcedure("MobileInfo_Update", parameters, out num);
        }
    }
}

